import { Debtor } from "../../../../../typings/types/debtor";
import { mainpersonAPI } from "../../../../api/debtor";
import { ZCC } from "../../../../utils/BasePage";

interface PageData {
  // page: number,
  companyId: string,
  // total: number | undefined,
  // items: Debtor.MainPerson[]
}
const colors = ['#91A8DE', '#DEC091', '#91BEDE', '#91DEAB', '#DE9191']
new ZCC.RefreshListPage<Debtor.MainPerson>({

  getColor(index: number): string {
    if (index < colors.length) { return colors[index] }
    const idx = index % colors.length
    return colors[idx]
  },
  getMainperson: async function () {

    this.setRefreshing()
    // wx.showLoading()
    try {
      console.log(this.data.page)
      const res = await mainpersonAPI(this.data.companyId, this.data.page);
      if (res.content) {
        const list = res.content.list
        console.log(res.content)
        let items: Debtor.MainPerson[] = []
        if (list.length > 0) {
          items = list.map((item, index) => {
            item.firstname = item.name.length > 0 ? item.name[0] : ''
            item.color = this.getColor(index)
            return item
          })
          // items = this.data.items.concat(list)
          // const page = ++this.data.page
          // this.setData({
          //   page: page,
          //   items: items,
          //   total: res.content.total || 0
          // })
        }
        // else {
        //   this.setData({
        //     total: res.content.total || 0
        //   })
        // }
        this.updateDatasWithNewItems(items, res.content.total, res.content.hasNextPage)
      }
    } finally {
      this.endRefreshing()
      // wx.stopPullDownRefresh()
      // wx.hideLoading()
    }
  },
  /**
   * 页面的初始数据
   */
  data: <PageData>{
    // page: 1,
    companyId: '',
    // total: undefined,
    // items: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.setData({
      companyId: this.options.companyId
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    this.getMainperson()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    // this.setData({
    //   page: 1,
    //   items: [],
    //   total: undefined
    // })
    if (this.resetPageIfNeed()) {
      this.getMainperson()
    }
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.getMainperson()
  },

})